<?php
namespace juven\libs {
    \defined( '_JEXEC' ) or die( 'Restricted access' );
    
    class juven {
        /** @var juven */
        protected static $_instance = \NULL;
        
        /** @var EventsManager */
        protected static $_eventsManager = \NULL;
        
        /** @var DatabaseAdapter */
        protected static $_db = \NULL;

        /** @var Router */
        protected static $_router = \NULL;
        
        /** @var Session */
// TO DELETE       protected static $_session = \NULL;

        /** @var User */
        protected static $_user = \NULL;

        /** @var Config */
        protected static $_config = \NULL;
        
        /** @var PermissionsManager */
        protected static $_permissionsManager = \NULL;
        
        /** @var array */
        protected static $_modules = array();

        /**
         * @param Config $configuration
         * @return juven
         */
        public static function Init(Config $configuration) {
            if( self::$_instance === \NULL ){ 
                self::$_instance = new self(); 
                self::$_config = $configuration;
                self::$_eventsManager = new EventsManager();
                self::$_db = new DatabaseAdapter();
                // @todo: here must load the static events and the modules
//                self::$_permissionsManager = new PermissionsManager();   //    TO MOVE TO CLASS ROUTER
// TO DELETE               self::$_session = new Session(self::$_db, self::$_config->db->connectionData, Tools::getPrefixedTableName());
                self::$_user = new User();
                return self::$_instance;
                self::$_router = new Router();
                self::$_router->route();
            }
            return self::$_instance;
        }

        /** @return juven */
        public static function getInstance(){ return self::$_instance; }
        
        /**
         * juven::getInstance()->module->function();
         * juven::getInstance()->module->variable;
         * 
         * Return Module instance or NULL if module name was not found
         * 
         * @param string $name The Module's name
         */
        public function __get($name) { if (array_key_exists($name, self::$_modules)){ return self::$_modules[$name]; } else { return \NULL; } }

        /** @return EventsManager */
        public static function eventsManager(){ return self::$_eventsManager; }
        
        /** @return juven\libs\DatabaseAdapter */
        public static function dataBase(){ return self::$_db; }
        
        /** @return juven\libs\Router */
        public static function router(){ return self::$_router; }
        
        /** @return juven\libs\Session */
// TO DELETE         public static function session(){ return self::$_session; }
        
        /** @return juven\libs\User */
        public static function user(){ return self::$_user; }
        
        /** @return juven\libs\Config */
        public static function config(){ return self::$_config; }
        
        /** @return PermissionsManager */
//        public static function permissionsManager(){ return self::$_permissionsManager; } //    TO MOVE TO CLASS ROUTER

        protected function __construct(){}
        protected function __clone(){}
    }
}
?>
